﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Web_rongmeiti_sys.PublishToRongMeiTi;

namespace Web_rongmeiti_sys.PublishToRongMeiTi
{
    /// <summary>
    /// html文本处理
    /// </summary>
    /// 2023-7-12 18:27:19  添加，作者:王浩力
    public class MyHtmlHelper
    {

        /// <summary>
        /// 处理文章内容，p标签style里面没有字体大小的设置补上设置，确保格式发布正确
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        /// 2023-7-12 18:27:19  添加，作者:王浩力
        public static string HtmlCheck(string html)
        {
            string htmlOrig = null;/* html原始值 */
            try
            {
                Span<char> newStr = new Span<char>(html.ToArray());
                htmlOrig = newStr.ToString();

                //LogHelpter.AddLog("发布前的原文:" + html);//发布前的原文 2020-6-16 14:14:01 添加
                html = html.Replace("<p", "\n<p").Replace("\">", "\">\n");

                //删除p标签里面没有字符内容的所有p标签对  2020-5-7 10:34:06 添加
                Regex regexP = new Regex("<p.*>\\s*(<.*>)*\\s*(<.*>)*\\s*</p>", RegexOptions.IgnoreCase);
                //html = regexP.Replace(html, "");
                var collon_first = regexP.Matches(html);
                int km = 0;     /* 有多少图片计数 */
                foreach (Match item in collon_first)
                {
                    string thtml = item.Value;

                    //包含文本，添加时间：2023-8-24 15:10:41，作者：王浩力
                    string mathValue = Regex.Replace(thtml, "<[^>]+>", "");
                    var findWords = Regex.Matches(mathValue, "[\\u4e00-\\u9fa5_a-zA-Z0-9]*").Where(g => g.Value != "");
                    int findLeng = findWords.Count();
                    if (findLeng>0)
                    {
                        km++;
                    }

                    //包含图片
                    if (thtml.ToLower().Contains("<img"))
                    {
                        km++;

                        //添加时间：2023-7-12 17:50:34，王浩力
                        //如果p标签内部有图片，则新增p的style样式，text-indent: 0 !important;
                        //防止融媒体中的图片，左侧有空白
                        if (thtml.Contains("text-indent"))
                        {
                            continue;
                        }

                        //// 移除样式text-indent: 2em;   //添加时间：2023-8-3 09:57:50，作者：王浩力
                        //string thtml_1 = Regex.Replace(thtml, "text-indent\\s*:\\s*\\d+(em)*;", "");
                        string thtml_1 = thtml;

                        int indexFirstStyle = thtml_1.IndexOf("<p style=\"");
                        if (indexFirstStyle>=0)
                        {
                            string thtmlNew = thtml_1.Insert(indexFirstStyle + 10, "text-indent: 0 !important;");
                            html = html.Replace(thtml, thtmlNew);
                        }
                    
                    }
                }
                if (km == 0)  /* 如果没有图片，则清理无内容标签 */
                {
                    html = regexP.Replace(html, "");
                }

                ////替换所有字体为宋体 2020-5-20 16:25:05 添加
                ////font-family:\s*[\u4E00-\u9FA5A-Za-z]+,?[\u4E00-\u9FA5A-Za-z]*;
                //Regex regex5 = new Regex("font-family:\\s*[\u4E00-\u9FA5A-Za-z]+,?[\u4E00-\u9FA5A-Za-z]*;", RegexOptions.IgnoreCase);
                //html = regex5.Replace(html, "font-family:SimSun;");

                //TO_P_WORK:

                //    html = html.Replace("<p>", "<p style=\"line-height:2em;font-family:SimSun;font-size:16px;\">");
                //    html = html.Replace("\n", "");
                return html;
            }
            catch (Exception ex1)
            {
                var ex = ex1.InnerException ?? ex1;
                string msg = ex.Message;
                LogHelpter.AddLog($"MyHtmlHelper.HtmlCheck处理异常,{msg}", null, "MyHtmlHelper_error");
            }
            return htmlOrig;
        }


    }
}
